﻿using UnityEngine;

namespace Script.Game
{
    public class FollowUI:MonoBehaviour
    {
        public Transform followTarget; // UI跟随的目标对象

        private RectTransform rectTransform; // UI的RectTransform组件

        private void Awake()
        {
            rectTransform = GetComponent<RectTransform>();
        }

        private void LateUpdate()
        {
            if (followTarget != null)
            {
                // rectTransform.anchoredPosition = Camera.main.WorldToScreenPoint(followTarget.position);
                //世界转屏幕 mainCamera世界的摄像机
                Vector3 screenPosition  = Camera.main.WorldToScreenPoint(followTarget.position);
                //屏幕转UI  ui(当前的canvas)  uiCamera(UI的摄像机)
                if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform.parent.GetComponent<RectTransform>(), screenPosition , SceneVariants.GetUICamera(), out var localPoint))
                {
                    rectTransform.anchoredPosition = localPoint;
                }
            }
        } 
    }
}